package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Teacher;
import com.geyue.core.model.Vedio;
import com.geyue.core.service.VedioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/4/24.
 */
@Controller
@RequestMapping("/vedio")
public class VedioController {

    @Autowired
    private VedioService vedioService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String list(Model model,HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "error";

        return "vedio_list";
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ResponseBody
    public Res page(Vedio vedio,Integer pageSize,Integer pageNum, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return Res.fail("登录信息过时,请重新登录!");

        vedio.setTeacherId(teacher.getId());
        return Res.success("",vedioService.getPage(pageNum,pageSize,vedio));
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model,HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "error";

        return "vedio_add";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res add(Vedio vedio,HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return Res.fail("登录信息过时,请重新登录!");

        vedio.setTeacherId(teacher.getId());
        if(vedioService.saveNotEmpty(vedio) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");

    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "error";

        model.addAttribute("info",vedioService.getOneByKey(id));
        return "vedio_edit";
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public Res edit(Vedio vedio,HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return Res.fail("登录信息过时,请重新登录!");

        if(vedioService.updateNotEmpty(vedio) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String info(@PathVariable Integer id, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return "error";

        model.addAttribute("info",vedioService.getOneByKey(id));
        return "vedio_info";
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Res del(@PathVariable Integer id, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");

        if(teacher == null)
            return Res.fail("登录信息过时,请重新登录!");

        if(vedioService.deleteByKey(id) > 0)
            return Res.success("删除成功!");

        return Res.fail("删除失败!");
    }

}
